const StateMan = require("stateman");
const PAGE_CONTAINER = "#main-container";

import ClubInfo from "../../modules/club_info/index.js";
import Department from "../../modules/department/index.js";
import DefaultActivity from "../../modules/default_activity/index.js";
import Header from "../header/index.js";
import Menu from "../menu/menu.js";

var stateman = new StateMan();

var club_id;
var clubs = [];

stateman
        .state("club", {
            enter: function(option){
                var headerCallback;
                console.log("entering club", option);
                if("club" === option.current.currentName){
                    headerCallback = function(clubs){
                        console.log("header callback");
                        club_id = clubs[0].club_id;
                        stateman.nav("/club/" + club_id + "/info/", {
                            id: club_id
                        });
                    };
                }else{
                    club_id = option.param.id;
                }
                new Header("#header", headerCallback);
                new Menu("#sidemenu", function(state){
                    stateman.nav("/club/" + club_id + "/" + state + "/", {
                        id: club_id
                    });
                });  
            },
            leave: function(option){
                console.log("leaving club", option);
            },
            update: function(option){
                console.log("updating club", option);
                if(option.is_redirect){
                    this.enter(option);
                }
            }
        })
        .state("club.detail", {
            url: ":id(\\d+)",
            enter: function(){
                console.log("entering club.detail");
            },
            leave: function(){
                console.log("leaving club.detail");
            },
            update: function(){
                console.log("updating club.detail");
            }
        })
        .state("club.detail.dept", {
            enter: function(option){
                console.log("entering club.detail.department", club_id);
                this.page = new Department(PAGE_CONTAINER, club_id);
            },
            update: function(){
                console.log("updating club.detail.department");
            },
            leave: function(){
                console.log("leaving club.detail.department");
                this.page.deconstructor();
            }
        })
        .state("club.detail.info", {
            enter: function(option){
                console.log("entering club.detail.info");
                this.page = new ClubInfo(PAGE_CONTAINER, club_id);
            },
            update: function(option){
                console.log("updating club.detail.info");
                if(option.param.id === club_id){
                    return;
                }
                // 更新id
                club_id = option.param.id;
                this.page.deconstructor();
                this.enter();
            },
            leave: function(){
                console.log("leaving club.detail.info");
                this.page.deconstructor();
            }
        })
        .state("club.detail.defaultactivity", {
            enter: function(option){
                console.log("entering club.detail.defaultActivity");
                this.page = new DefaultActivity(PAGE_CONTAINER, club_id);
            },
            update: function(){
                console.log("updating club.detail.defaultActivity");
            },
            leave: function(){
                console.log("leaving club.detail.defaultActivity");
                this.page.deconstructor();
            }
        }).on("notfound", function(){
            this.go("club", {
                is_redirect: true
            });
            
        }).on("ACCESS_DENYED", function(){
            stateman.go("club", {
                is_redirect: true
            });
        });

export default stateman;